package com.fc.mall.controller;

import com.fc.mall.base.object.Result;
import com.fc.mall.dto.CommonResult;
import com.fc.mall.dto.DeliveryTempParams;
import com.fc.mall.service.PmsDeliveryTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Sunny on 2019/6/23.
 */
@RestController
@Api(tags = "PmsDeliveryTemplateController", description = "运费模板管理")
@RequestMapping("/delivery")
public class PmsDeliveryTemplateController {

    @Autowired
    private PmsDeliveryTemplateService deliveryTemplateService;

    @GetMapping("list")
    public CommonResult list(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        return new CommonResult().pageSuccess(deliveryTemplateService.list());
    }


    @PostMapping
    public Result add(@RequestBody DeliveryTempParams deliveryTempParams)
    {
        deliveryTemplateService.addOrUpdateDelivery(deliveryTempParams);
        return Result.success();
    }


    @GetMapping("/{deliveryId}")
    public Result get(@PathVariable(name = "deliveryId") Long deliveryId)
    {
        return Result.success(deliveryTemplateService.get(deliveryId));
    }

    @PutMapping
    public Result update(@RequestBody DeliveryTempParams deliveryTempParams)
    {
        deliveryTemplateService.addOrUpdateDelivery(deliveryTempParams);
        return Result.success();
    }

    @DeleteMapping("/{deliveryId}")
    public Result del(@PathVariable(name = "deliveryId") Long deliveryId)
    {
        deliveryTemplateService.del(deliveryId);
        return Result.success();
    }

    @ApiOperation("运费模板克隆")
    @PostMapping("clone")
    public Result deliveryClone (@RequestBody DeliveryTempParams params){
        DeliveryTempParams deliveryTempParams = deliveryTemplateService.get(params.getId());
        deliveryTempParams.setId(null);
        deliveryTempParams.setName(params.getName());
        deliveryTemplateService.addOrUpdateDelivery(deliveryTempParams);
        return Result.success();
    }
}
